/** @type {import('next').NextConfig} */
const path = require('path')
const TerserPlugin = require('terser-webpack-plugin');

const nextConfig = {
  reactStrictMode: true,
  poweredByHeader: false,
  productionBrowserSourceMaps: false,
  compress: true,
  webpack: (config) => {
    if (process.env.NEXT_PUBLIC_PROD) {
      config.optimization.minimize = true;
      config.optimization.minimizer = [new TerserPlugin({
        terserOptions: {
          compress: {
            drop_console: true,   //删除所有console
          },
          output: {
            comments: false,      //删除所有注释
          },
        },
        extractComments: false,   //禁止terser将注释提取到单独的文件中
      })];
    }

    // 公共代码提取
    config.optimization.splitChunks = {
      chunks: 'initial',
      minSize: 30000, 
      maxSize: 100000, 
      cacheGroups: {
        default: {
          name: 'common',
          minChunks: 2,
          priority: -20,
        },
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          name: 'vendor',
          priority: -10,
        }
      }
    },
    config.plugins.DefinePlugin = {
      'NEXT_PUBLIC_ENV': process.env.NEXT_PUBLIC_ENV,
      'NEXT_PUBLIC_BAN_CONSOLE': process.env.NEXT_PUBLIC_BAN_CONSOLE,
    }
    config.resolve.alias['@'] = path.resolve(__dirname, 'src')
    config.resolve.alias['@c'] = path.resolve(__dirname, 'components')
    return config
  },
  output: 'standalone',
  eslint: {
    ignoreDuringBuilds: true,
  },
}

module.exports = nextConfig